package com.boyadegroot.divelog.ui.diver.tabs;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.boyadegroot.divelog.R;
import com.boyadegroot.divelog.data.DiveLogData;
import com.boyadegroot.divelog.data.DiveLogDatabaseContract;
import com.boyadegroot.divelog.data.DiveLogProvider;
import com.boyadegroot.divelog.models.Dive;
import com.boyadegroot.divelog.models.Diver;
import com.boyadegroot.divelog.ui.DiveDetails;

public class DiverDives extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {

    private static final String TAG = DiverDives.class.getName();

    private Diver mDiver;
    private DiverDivesAdapter mAdapter;

    public static DiverDives newInstance(Diver diver) {
        // Log.v(TAG, "DiverDives newInstance(diver = " + diver.getUsername() + ")");
        DiverDives instance = new DiverDives();
        Bundle args = new Bundle();
        args.putParcelable("diver", diver);
        instance.setArguments(args);
        return instance;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // Log.v(TAG, "onCreate(Bundle savedInstanceState)");
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        if (args != null) {
            mDiver = getArguments().getParcelable("diver");
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Context context = getActivity();
        mAdapter = new DiverDivesAdapter(context, R.layout.listitem_dive);
        setListAdapter(mAdapter);
        ListView divesList = getListView();
        divesList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Activity activity = getActivity();
                Context context = activity.getApplicationContext();
                Intent intent = new Intent(context, DiveDetails.class);
                Long diveId = (Long) view.getTag(R.id.tag_dive_id);
                Integer diveNumber = (Integer) view.getTag(R.id.tag_dive_number);
                DiveLogData data = DiveLogData.getInstance(context);
                Dive dive = data.getDive(diveId);
                intent.putExtra("number", diveNumber);
                intent.putExtra("dive", dive);
                startActivity(intent);
            }
        });
        getLoaderManager().initLoader(0, null, this);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        // Log.v(TAG, "onLoadFinished(Loader<Cursor> loader, Cursor data)");
        mAdapter.swapCursor(data);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        // Log.v(TAG, "onLoaderReset(Loader<Cursor> loader)");
        mAdapter.swapCursor(null);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        // Log.v(TAG, "Loader<Cursor> onCreateLoader(int id, Bundle args)");
        Context context = getActivity();
        String diverId = Long.toString(mDiver.getId());
        Uri divesUri = Uri.withAppendedPath(DiveLogProvider.CONTENT_URI, DiveLogProvider.PATH_DIVES);
        CursorLoader cursorLoader = new CursorLoader(
                context,
                divesUri,
                new String[]{ // columns to request in the query
                        DiveLogDatabaseContract.Dives._ID,
                        DiveLogDatabaseContract.Dives.COLUMN_NAME_SITE_ID,
                        DiveLogDatabaseContract.Dives.COLUMN_NAME_STARTTIME
                },
                DiveLogDatabaseContract.Dives.COLUMN_NAME_DIVER_ID + " = ?",
                new String[]{diverId},
                DiveLogDatabaseContract.Dives.COLUMN_NAME_STARTTIME);
        // Log.v(TAG, "Creating loader for diver with id = " + diverId);
        return cursorLoader;
    }
}